#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>

int main(){
    int status;
    printf("Running ls with system\n");
    //前台执行的程序system会等待进程执行结束后才返回
    system("ls -l &");
    //后台执行的程序system会立刻返回
    //system("ls -l");
    printf("system函数执行结束\n");
    if(WIFEXITED(status))
    {
        printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); //取得cmdstring执行结果
    }
    else if(WIFSIGNALED(status))
    {
        printf("abnormal termination,signal number =%d\n", WTERMSIG(status)); //如果cmdstring被信号中断，取得信号值
    }
    else if(WIFSTOPPED(status))
    {
        printf("process stopped, signal number =%d\n", WSTOPSIG(status)); //如果cmdstring被信号暂停执行，取得信号值
    }
	exit(0);
}


Running ls with system
system函数执行结束
normal termination, exit status = 0
Press <RETURN> to close this window...
total 60
-rw-rw-r-- 1 danieldai danieldai  4520 Mar 23 07:07 main.o
-rw-rw-r-- 1 danieldai danieldai 39371 Mar 23 07:01 Makefile
-rwxrwxr-x 1 danieldai danieldai 10072 Mar 23 07:07 system


#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    int status;
    printf("Running ls with system\n");
    //前台执行的程序system会等待进程执行结束后才返回
//    system("ls -l &");
    //后台执行的程序system会立刻返回
    system("ls -l");
    printf("system函数执行结束\n");
    if(WIFEXITED(status))
    {
        printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); //取得cmdstring执行结果
    }
    else if(WIFSIGNALED(status))
    {
        printf("abnormal termination,signal number =%d\n", WTERMSIG(status)); //如果cmdstring被信号中断，取得信号值
    }
    else if(WIFSTOPPED(status))
    {
        printf("process stopped, signal number =%d\n", WSTOPSIG(status)); //如果cmdstring被信号暂停执行，取得信号值
    }
    exit(0);
}
Running ls with system
total 60
-rw-rw-r-- 1 danieldai danieldai  4520 Mar 23 07:09 main.o
-rw-rw-r-- 1 danieldai danieldai 39371 Mar 23 07:01 Makefile
-rwxrwxr-x 1 danieldai danieldai 10072 Mar 23 07:09 system
system函数执行结束
normal termination, exit status = 0
Press <RETURN> to close this window..

